declare const document: Document
const tools = {
  // 获取 cookie
  getCookie(key: string) {
    const cookies = document.cookie.split('; ')
        type MyObject = {
            [key: string]: any
        }
        if (!key) {
          const result:MyObject = {}
          for (let i = 0; i < cookies.length; i += 1) {
            const [name, value] = cookies[i].split('=')
            result[name] = value
          }
          return result
        }
        for (let i = 0; i < cookies.length; i += 1) {
          const [name, value] = cookies[i].split('=')
          if (name === key) {
            return value
          }
        }
        return null // 添加错误处理
  },
  // 设置cookie
  setCookie(key:any, value:any, expires:any, path:any) {
    // 1.准备一个标准 cookie 内容
    let str = `${key}=${value}`
    if (expires) {
      const time = new Date()
      time.setTime(time.getTime() - 1000 * 60 * 60 * 8 + expires * 1000)
      str += `;expires=${time}`
    } else {
      str += `;path=${path}`
    }
    document.cookie = str
  },
}

export default tools
